<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/7/27
 * Time: 9:45
 */

namespace app\admin\controller;

\think\Loader::import('controller/Controller', \think\Config::get('traits_path') , EXT);

use app\admin\Controller;
use think\Db;
use think\Request;

class Booking extends Controller
{
    use \app\admin\traits\controller\Controller;
    /**
     * 预订房间
     * @return mixed
     */
    public function index(Request $request)
    {

        $data = input();
//        //搜索
        //订单ID
        if ($this->request->param("order_id")) {
            $map['o.order_id'] = array('like', "%{$this->request->param("order_id")}%");
        }

        //预订人
        if ($data['booking_people_name']) {
            $map['o.booking_people_name'] = array('like', "%{$data['booking_people_name']}%");
        }

        //预订人电话
        if ($data['booking_people_phone']) {
            $map['o.booking_people_phone'] = array('like', "%{$data['booking_people_phone']}%");
        }


        //平台
        if ($data['platform']) {
            $map['o.platform'] = $data['platform'];
        }

        //出行日期
        if ($data['start_time'] && $data['end_time']) {
            $map['o.start_time'] = array("between", [strtotime($data['start_time'] . " 00:00:00"), strtotime($data['end_time'] . " 23:59:59")]);
        } elseif ($data['start_time']) {
            $map['o.start_time'] = array("between", [strtotime($data['start_time'] . " 00:00:00"), strtotime($data['start_time'] . " 23:59:59")]);
        } elseif ($data['end_time']) {
            $map['o.start_time'] = array("between", [strtotime($data['end_time'] . " 00:00:00"), strtotime($data['end_time'] . " 23:59:59")]);
        }

        //预订人备注
        if ($data['booking_people_remark']) {
            $map['o.booking_people_remark'] = array('like', "%{$data['booking_people_remark']}%");
        }

        $listRows = $this->request->param('numPerPage') ?: config("paginate.list_rows");

        $map['o.dispose_status'] = 'vehicle_dispatching';
        $map['ot.group_id'] = 0;
        $map['ot.is_error'] = 0;

        //查询订单信息
        $list = Db::name('order_traveler')
            ->alias("ot")
            ->join("order o", "o.id=ot.oid", 'left')
            ->join("portion_traveler pt", "o.id=pt.oid", 'left')
            ->where($map)
            ->where('ot.isaccommodation','包含住宿')
            ->where(function ($query){
                $query->whereNull('pt.isbooking')
                    ->whereOr('pt.isbooking',0);
            })
            ->group('ot.oid')
            ->order('ot.id desc')
            ->field("ot.*,o.*")
            ->paginate($listRows, false, ['query' => $this->request->get()]);
        $new_list = $list->toArray()['data'];
        foreach ($new_list as $key => $item) {
            if (empty($item['id'])) {
                unset($new_list[$key]);
            } else {
                $new_list[$key]['order_remark'] = Db::name('order_remark')
                    ->where('order_id', $item['order_id'])
                    ->order('id desc')
                    ->select();
                continue;
            }
        }

        $oids = [];
        $order_travelers = [];
        //整合订单ids
        foreach ($new_list as $item) {

            if ($oids[$item['order_id']] != true) {

                $travelers = $item;
                $arr = Db::name('order_traveler')->where('oid', $item['id'])->field('id')->select();
                $travelers['num'] = count($arr);
                $travelers['show_single'] = true;
                $travelers['show_mian'] = false;
                $travelers['show_between'] = false;
                $travelers['show_end'] = false;
                $travelers['route_title'] = Db::name('route')
                    ->where('id', $item['route_id'])
                    ->value('title');
                //获取该订单的旅客信息
                $travelers_info = Db::name('order_traveler')->where('oid', $item['id'])->select();
                $arr = [];
                foreach ($travelers_info as $k => $v) {
                    $arr[] = $v['id'] . '-' . $v['name'];
                }
                $info = implode(',', $arr);
                $travelers['travelers_info'] = $info;
                $travelers['travelers_info_list'] = $travelers_info;
                //查询该订单的关联订单id
                $relevance_id = Db::name('order')->where('id', $item['id'])->value('relevance_id');
                //获取订单ids
                $order_ids = Db::name('order_relevance')->where('relevance', $relevance_id)->field('order_id')->select();
                $relevance_order_list = [];
                foreach ($order_ids as $k => $v) {
                    if ($v['order_id'] != $item['id']) {
                        $relevance = Db::name('order')->where('id', $v['order_id'])->find();
                        //获取关联订单的旅客信息
                        $relevance_travelers_info = Db::name('order_traveler')->where('oid', $v['order_id'])->select();
                        $relevance_travelers_info = Db::name('order_traveler')->where('oid', $v['order_id'])->select();
                        $relevance['num'] = count($relevance_travelers_info);
                        $arrs = [];
                        foreach ($relevance_travelers_info as $kt => $vt) {
                            $arrs[] = $vt['id'] . '-' . $vt['name'];
                        }
                        $relevance_info = implode(',', $arrs);
                        $relevance['travelers_info'] = $relevance_info;
                        $relevance['travelers_info_list'] = $relevance_travelers_info;
                        $relevance['order_remark'] = Db::name('order_remark')
                            ->where('order_id', $v['order_id'])
                            ->order('id desc')
                            ->select();
                        $relevance_order_list[] = $relevance;
                    }
                }
                $travelers['relevance_order_list'] = $relevance_order_list;
                $order_travelers[] = $travelers;
                $oids[$item['order_id']] = true;

            }

        }

//        dump($order_travelers);
        // 模板赋值显示
        $platform = Db::name('platform')->select();
        $this->view->assign('Request.param.ctime', $data);
        $this->view->assign('Request.param.start_time', $data);
        $this->view->assign('platform', $platform);
        $this->view->assign('list', $order_travelers);
        if (!empty($order_travelers)) {
            $this->view->assign("count", count($order_travelers));
        }
        $this->view->assign("page", $list->render());
        $this->view->assign('numPerPage', $list->listRows());

        if ($request->isAjax()) {
            $this->view->assign('refresh', 1);
            return $this->view->fetch('ajaxpage');
        } else {
            return $this->view->fetch('undistributed_traveller');
        }
    }

}